package com.example.demo.controller;

import com.example.demo.log.Log;
import com.example.demo.log.LogModuleEnum;
import com.example.demo.model.core.Add;
import com.example.demo.model.core.PageVo;
import com.example.demo.model.core.R;
import com.example.demo.model.core.Update;
import com.example.demo.model.form.DictForm;
import com.example.demo.model.form.DictItemForm;
import com.example.demo.model.form.DictItemSaveForm;
import com.example.demo.model.query.DictItemQuery;
import com.example.demo.model.query.DictQuery;
import com.example.demo.model.vo.DictItemVO;
import com.example.demo.model.vo.DictVO;
import com.example.demo.model.vo.OptionVO;
import com.example.demo.service.SysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/dict")
@Tag(name = "字典接口")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('sys:dict:add')")
    @Operation(summary = "新增字典")
    @Log(value = "新增字典", module = LogModuleEnum.DICT)
    public R<Void> add(@Validated(Add.class) @RequestBody DictForm form) {
        sysDictService.add(form);
        return R.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('sys:dict:edit')")
    @Operation(summary = "修改字典")
    @Log(value = "修改字典", module = LogModuleEnum.DICT)
    public R<Void> update(@Validated(Update.class) @RequestBody DictForm form) {
        sysDictService.update(form);
        return R.ok();
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPerm('sys:dict:delete')")
    @Operation(summary = "删除字典")
    @Log(value = "删除字典", module = LogModuleEnum.DICT)
    public R<Void> delete(@RequestBody List<Long> ids) {
        sysDictService.delete(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:dict:query')")
    @Operation(summary = "字典列表")
    public R<PageVo<DictVO>> page(@Validated DictQuery query) {
        PageVo<DictVO> page = sysDictService.page(query);
        return R.ok(page);
    }

    @GetMapping("/form")
    @Operation(summary = "字典表单")
    public R<DictForm> form(Long id) {
        DictForm form = sysDictService.form(id);
        return R.ok(form);
    }


    @GetMapping("/items/page")
    @Operation(summary = "字典项列表")
    public R<PageVo<DictItemVO>> page(@Validated DictItemQuery query) {
        PageVo<DictItemVO> page = sysDictService.pageItem(query);
        return R.ok(page);
    }

    @PostMapping("/items/add")
    @Operation(summary = "新增字典项")
    @Log(value = "新增字典项", module = LogModuleEnum.DICT)
    public R<Void> add(@Validated(Add.class) @RequestBody DictItemForm form) {
        sysDictService.addItem(form);
        return R.ok();
    }

    @PostMapping("/items/update")
    @Operation(summary = "修改字典项")
    @Log(value = "修改字典项", module = LogModuleEnum.DICT)
    public R<Void> update(@Validated(Update.class) @RequestBody DictItemForm form) {
        sysDictService.updateItem(form);
        return R.ok();
    }

    @PostMapping("/items/delete")
    @Operation(summary = "删除字典项")
    @Log(value = "删除字典项", module = LogModuleEnum.DICT)
    public R<Void> itemsDelete(@RequestBody List<Long> ids) {
        sysDictService.deleteItem(ids);
        return R.ok();
    }

    @GetMapping("/items/form")
    public R<DictItemForm> itemsForm(String dictCode, Long id) {
        DictItemForm form = sysDictService.formItem(id);
        return R.ok(form);
    }

    @PostMapping("/items/save")
    @Operation(summary = "字典项保存")
    @Log(value = "字典项保存", module = LogModuleEnum.DICT)
    public R<Void> itemsSave(@RequestBody DictItemSaveForm form) {
        sysDictService.itemsSave(form);
        return R.ok();
    }

    @GetMapping("/items/{dictCode}")
    public R<List<OptionVO<String>>> itemsForm(@PathVariable("dictCode") String dictCode) {
        List<OptionVO<String>> dictItemVos = sysDictService.itemsOption(dictCode);
        return R.ok(dictItemVos);
    }
}
